package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.VaccineType;
import com.example.service.IVaccineTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 疫苗分类 前端控制器
 * </p>
 *
 * @author wyx
 * @since 2025-03-19
 */
@CrossOrigin//跨域访问
@Slf4j
@RestController
@Api(tags = "疫苗分类管理模块")
@RequestMapping("/vaccineType")
public class VaccineTypeController {
    @Autowired
    IVaccineTypeService vaccineTypeService;

    //分页展示疫苗分类列表
    @RequestMapping("/page")
    public R page(Integer page, Integer pageSize, String name, Integer status) {
        log.info("分页查询疫苗分类列表page:{},pageSize:{},name:{},status:{}", page, pageSize, name, status);
        Page<VaccineType> pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<VaccineType> queryWrapper = new LambdaQueryWrapper<>();
        if (name != null) {
            queryWrapper.like(VaccineType::getName, name);
        }
        if (status != null) {
            queryWrapper.eq(VaccineType::getStatus, status);
        }

        return R.success(vaccineTypeService.page(pageInfo, queryWrapper));
    }

    // 新增疫苗类型
    @ApiOperation(value = "新增疫苗类型")
    @PostMapping("/add")
    public R add(VaccineType vaccineType) {
        log.info("新增疫苗类型: {}", vaccineType);

        boolean result = vaccineTypeService.save(vaccineType);

        if (result) {
            return R.success("新增成功");
        } else {
            return R.error("新增失败");
        }
    }

    // 修改疫苗类型
    @ApiOperation(value = "修改疫苗类型")
    @PutMapping("/update/{id}")
    public R update(@PathVariable Integer id, VaccineType vaccineType) {
        log.info("更新疫苗类型: {}", id);

        // 根据ID查询疫苗类型信息
        VaccineType existingVaccineType = vaccineTypeService.getById(id);
        if (existingVaccineType == null) {
            return R.error("疫苗类型不存在");
        }

        // 设置需要更新的字段
        existingVaccineType.setName(vaccineType.getName());
        existingVaccineType.setRemark(vaccineType.getRemark());
        existingVaccineType.setStatus(vaccineType.getStatus());

        boolean result = vaccineTypeService.updateById(existingVaccineType);

        if (result) {
            return R.success("更新成功");
        } else {
            return R.error("更新失败");
        }
    }

    // 删除疫苗类型
    @ApiOperation(value = "删除疫苗类型")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id) {
        log.info("删除疫苗类型: {}", id);

        boolean result = vaccineTypeService.removeById(id);

        if (result) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @ApiOperation(value = "更新疫苗类型状态")
    @PutMapping("/updateStatus/{id}")
    public R updateStatus(@PathVariable Integer id, Integer status) {
        log.info("更新疫苗类型状态: {}", id);

        // 根据ID查询疫苗类型信息
        VaccineType existingVaccineType = vaccineTypeService.getById(id);
        if (existingVaccineType == null) {
            return R.error("疫苗类型不存在");
        }

        // 更新状态
        existingVaccineType.setStatus(status);

        boolean result = vaccineTypeService.updateById(existingVaccineType);

       if (result) {
           return R.success("状态更新成功");
       }else {
           return R.error("状态更新失败");
       }
    }
}
